<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 *
 * PHP version 5
 *
 * Copyright 2008 The Picnic PHP Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *            http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @category            Framework
 * @package             Pfw
 * @author              Sean Sitter <sean@picnicphp.com>
 * @copyright           2008 The Picnic PHP Framework
 * @license             http://www.apache.org/licenses/LICENSE-2.0
 * @link                http://www.picnicphp.com
 * @since               0.10
 */

require('../../Pfw/UnitTest/PHPUnit/FwkTestCase.php');
Pfw_Loader::loadClass('Pfw_Paginator_SelectBuilder');

class Pfw_Paginator_SelectBuilder_Test extends Pfw_UnitTest_PHPUnit_FwkTestCase
{
    public function setup()
    {
        parent::setup();
        $this->createTableAndData();
    }

    public function  tearDown()
    {
        parent::tearDown();
        $this->dropTable();
    }
    
    public function testPaginate()
    {
        $db = Pfw_Db::factory();
        $select = $db->getSelectBuilder();
        $select->from('paginate', array('*'));
        
        $paginator = new Pfw_Paginator_SelectBuilder($select, 2, 10);
        $rs = $paginator->paginate();
        $this->assertEquals(10, count($rs));
        $this->assertEquals(11, $paginator->getLastPageNum());
        $this->assertEquals(110, $paginator->getTotal());
        $this->assertEquals($rs[0]['name'], "testname10");
        $this->assertEquals($rs[0]['test_id'], 11);
    }

    public function createTableAndData()
    {
        $db = Pfw_Db::factory();
        $db->insert("
CREATE TABLE IF NOT EXISTS `paginate` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `test_id` int(11) UNSIGNED NOT NULL,
  `name` varchar(32) DEFAULT NULL,
  `value` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1");
        
        $data = array();
        for ($i = 0; $i < 110; $i++) {
            array_push($data, array(
                'test_id' => $i + 1,
                'name' => "testname{$i}",
                'value' => "testvalue{$i}"
            ));
        }
        $db->insertAllIntoTable('paginate', $data);
    }

    public function dropTable()
    {
        $db = Pfw_Db::factory();
        $db->insert("DROP TABLE IF EXISTS paginate");
    }
}

?>
